package com.tiza.xiaoyoubao.ui.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tianze.library.base.BaseFragment;
import com.tianze.library.view.SimpleDialog;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.utils.ImageUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

import static android.app.Activity.RESULT_OK;

/**
 * Created by mapple on 17/3/19.
 * 绑定蘑菇街账号
 */

public class MoGuJieBindingFragment extends BaseFragment {

    /**
     * 选择图片的返回码
     */
    public final static int SELECT_IMAGE_RESULT_CODE = 200;
    String[] proj = {MediaStore.MediaColumns.DATA};
    /**
     * 当前选择的图片的路径
     */
    public String mImagePath;
    @BindView(R.id.tv_text)
    TextView tv_text;
    @BindView(R.id.iv_zhengshu)
    ImageView iv_zhengshu;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mogujie;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        SpannableString spannableString = new SpannableString(
                mActivity.getString(R.string.text_notice));
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#ff3a33"));
        ForegroundColorSpan colorSpan1 = new ForegroundColorSpan(Color.parseColor("#ff3a33"));
        spannableString.setSpan(colorSpan, 6, 21, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(colorSpan1, spannableString.length() - 5,
                                spannableString.length() - 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE
        );
        tv_text.setText(spannableString);
    }
    @OnClick({R.id.view_credibility,R.id.tv_chooseCity})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.view_credibility:
                showCustomConfirmDialog("请选择上传方式", "手机相册", "手机拍照",
                                        new SimpleDialog.OnSimpleClickListener() {
                                            @Override
                                            public void onClick(SimpleDialog simpleDialog) {
                                                Intent intent = new Intent(
                                                        Intent.ACTION_GET_CONTENT);
                                                pickPhoto();
                                                simpleDialog.dismiss();
                                            }
                                        }, new SimpleDialog.OnSimpleClickListener() {
                            @Override
                            public void onClick(SimpleDialog simpleDialog) {
                                takePhoto();
                                simpleDialog.dismiss();
                            }
                        }
                );
                break;
            case R.id.tv_chooseCity:
                UIHelper.showGetCity(mActivity);
                break;
            default:
                break;
        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SELECT_IMAGE_RESULT_CODE && resultCode == RESULT_OK) {
            String imagePath = "";
            Uri uri = null;
            if (data != null && data.getData() != null) {// 有数据返回直接使用返回的图片地址
                uri = data.getData();
                Cursor cursor = getActivity().getContentResolver().query(uri, proj, null, null,
                                                                         null
                );
                if (cursor == null) {
                    uri = ImageUtils.getUri(getActivity(), data);
                }
                imagePath = ImageUtils.getFilePathByFileUri(getActivity(), uri);
            } else {// 无数据使用指定的图片路径
                imagePath = mImagePath;
                int i = ImageUtils.parseImageDegree(imagePath);
                System.out.println("旋转的角度:" + i);

            }
            //获取图片缩略图，避免OOM
            Bitmap bitmap = ImageUtils.getImageThumbnail(imagePath, ImageUtils.getWidth(
                    getActivity().getApplicationContext()) / 3 - 5, ImageUtils.getWidth(
                    getActivity().getApplicationContext()) / 3 - 5);
            Bitmap bitmap1 = ImageUtils.rotateBitmap(bitmap,
                                                     ImageUtils.parseImageDegree(imagePath)
            );
            iv_zhengshu.setImageBitmap(bitmap1);
        }
    }

    /***
     * 从相册中取图片
     */
    public void pickPhoto() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        getActivity().startActivityForResult(intent, SELECT_IMAGE_RESULT_CODE);
    }

    /**
     * 拍照获取图片
     */
    public void takePhoto() {
        // 执行拍照前，应该先判断SD卡是否存在
        String SDState = Environment.getExternalStorageState();
        if (SDState.equals(Environment.MEDIA_MOUNTED)) {
            /**
             * 通过指定图片存储路径，解决部分机型onActivityResult回调 data返回为null的情况
             */
            //获取与应用相关联的路径
            String imageFilePath = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES)
                                                .getAbsolutePath();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA);
            //根据当前时间生成图片的名称
            String timestamp = "/" + formatter.format(new Date()) + ".jpg";
            File imageFile = new File(imageFilePath, timestamp);// 通过路径创建保存文件
            mImagePath = imageFile.getAbsolutePath();
            Uri imageFileUri = Uri.fromFile(imageFile);// 获取文件的Uri
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);// 告诉相机拍摄完毕输出图片到指定的Uri
            getActivity().startActivityForResult(intent, SELECT_IMAGE_RESULT_CODE);
        } else {
            toast("内存卡不存在");
        }
    }
}
